package com.bw.goods.mvvm.bind;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bw.goods.R;
import com.bw.goods.mvvm.adapter.HomeBannerAdapter;
import com.bw.goods.mvvm.adapter.HomeHotAdapter;
import com.bw.goods.mvvm.adapter.HomeMenuAdapter;
import com.bw.goods.mvvm.adapter.HomeTopAdapter;
import com.bw.goods.mvvm.adapter.HomeTxtBannerAdapter;
import com.bw.goods.mvvm.model.entity.HomeBannerEntity;
import com.bw.goods.mvvm.model.entity.HomeMenuEntity;
import com.bw.mvvm_common.BaseApplication;
import com.bw.mvvm_common.field.UIField;
import com.bw.skill.mvvm.view.SkillActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.youth.banner.Banner;

import java.util.HashMap;
import java.util.List;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/14 0014 下午 7:25
 *
 */
class GoodsRvBind {
    @BindingAdapter(value = {"goodsadapter","imagebanner",
            "txtbanner","menudata","hotdata","topEntities","transformer"})
    public static void bindGoodsRv(RecyclerView recyclerView,
                                   BaseQuickAdapter goodsadapter,
                                   List<HomeBannerEntity> imagebanner,
                                   List<String>txtbanner,
                                   List<HomeMenuEntity>menudata,
                                   List<String>hotdata,
                                   List<HomeBannerEntity>topEntities,
                                   ViewPager.PageTransformer transformer){
        //线性
//        LinearLayoutManager lmanager = new LinearLayoutManager(recyclerView.getContext());
//        lmanager.setOrientation(LinearLayoutManager.VERTICAL);
//        recyclerView.setLayoutManager(lmanager);
        //网格
        GridLayoutManager manager1 = new GridLayoutManager(recyclerView.getContext(), 2);
        manager1.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager1);

        View inflate = LayoutInflater.from(recyclerView.getContext()).inflate(R.layout.view_home_banner, null);
        //顶部banner
        Banner home_view_banner = inflate.findViewById(R.id.home_view_banner);
        home_view_banner.setAdapter(new HomeBannerAdapter(imagebanner))
                .setOrientation(Banner.HORIZONTAL)
                .start();
        //文字banner
        Banner home_text_banner = inflate.findViewById(R.id.home_text_banner);
        home_text_banner.setAdapter(new HomeTxtBannerAdapter(txtbanner))
                .setOrientation(Banner.VERTICAL)
                .start();
        //菜单
        RecyclerView home_menu_tv = inflate.findViewById(R.id.home_menu_rv);
        GridLayoutManager manager = new GridLayoutManager(recyclerView.getContext(),4);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        home_menu_tv.setLayoutManager(manager);
        HomeMenuAdapter homeMenuAdapter = new HomeMenuAdapter();
        homeMenuAdapter.setNewInstance(menudata);
        home_menu_tv.setAdapter(homeMenuAdapter);
//        homeMenuAdapter.setOnItemClickListener(new OnItemClickListener() {
//            @Override
//            public void onItemClick( BaseQuickAdapter<?, ?> adapter, View view, int position) {
//                if (menudata.get(position).pagePath.equals(R.drawable.ic_yundong)){
//                    BaseApplication.getApplication().startActivity(new Intent(BaseApplication
//                            .getApplication(),SkillActivity.class));
//
//                }
//
//            }
//        });

        //热门
        RecyclerView home_hot_rv = inflate.findViewById(R.id.home_hot_rv);
        LinearLayoutManager hotManager = new LinearLayoutManager(recyclerView.getContext());
        hotManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        home_hot_rv.setLayoutManager(hotManager);
        HomeHotAdapter homeHotAdapter = new HomeHotAdapter();
        homeHotAdapter.setNewInstance(hotdata);
        home_hot_rv.setAdapter(homeHotAdapter);

        //画廊效果
        ViewPager home_vp = inflate.findViewById(R.id.home_vp);
        home_vp.setOffscreenPageLimit(4);
        home_vp.setPageTransformer(true,transformer);
        home_vp.setAdapter(new HomeTopAdapter(topEntities));


        goodsadapter.addHeaderView(inflate);
        recyclerView.setAdapter(goodsadapter);


    }

    @BindingAdapter(value = "adapter")
    public static void bindAdapter(RecyclerView recyclerView, BaseQuickAdapter adapter){
        if (adapter == null)
            return;
        recyclerView.setAdapter(adapter);
    }

    @BindingAdapter(value = "orientation")
    public static void bindLinearManager(RecyclerView recyclerView,int orientation){
        LinearLayoutManager manager = new LinearLayoutManager(recyclerView.getContext());
        manager.setOrientation(orientation);
        recyclerView.setLayoutManager(manager);
    }

    @BindingAdapter(value = {"gridorientation","rvnum"})
    public static void bindGridManager(RecyclerView recyclerView,int gridorientation,int rvnum){
        GridLayoutManager manager = new GridLayoutManager(recyclerView.getContext(),rvnum);
        manager.setOrientation(gridorientation);
        recyclerView.setLayoutManager(manager);
    }
}
